<template>
  <div class="custom-body" :style="borderStyle">
    <div class="body-custom">
      <div v-if="showHeader" class="custom-header" :style="titleStyle">
        <div class="header-body" :style="headerStyle">
          <div v-if="temp.sfsxtb === true" class="header-icon">
            <i :class="temp.tb" />
          </div>
          <div v-else class="header-line" />
          <div class="header-title">
            <span class="title-span">{{ temp.name }}</span>
          </div>
          <div
            v-if="temp.isMore === true"
            class="header-icon more"
            :style="titleFont"
            @click="callBack"
          >
            更多
          </div>
        </div>
      </div>
      <div class="custom-content">
        <el-empty
          v-if="dataList === null || dataList.length === 0"
          :image-size="100"
        />
        <div v-else>
          <div
            v-for="(obj, index) in dataList"
            :key="'obj-' + index"
            class="custom-ul-li"
            :style="active === index ? activeStyle : ''"
            :loading="loading"
            @mouseenter="openAlert(index)"
            @mouseleave="closeAlert(index)"
            @click="openEntity(obj)"
          >
            <el-tooltip :content="obj[column['title']]" placement="top">
              <div
                class="custom-ul-li-title"
                style="
                overflow: hidden;
                display:-webkit-box;
                text-overflow: ellipsis;
                -webkit-line-clamp: 1;
                -webkit-box-orient: vertical;
                white-space: normal;
              "
              >
                {{ obj[column["title"]] }}
              </div>
            </el-tooltip>

            <div class="custom-li-button">
              {{
                obj[column["left"]].substring(0, obj[column["left"]].length - 3)
              }}
            </div>
            <!-- <div class="custom-ul-li-button">

              <div class="custom-li-time">{{ obj[column["time"]] }}</div>
            </div> -->
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import {
  queryAllSystemData,
  queryAllGuestSystemData
} from '@/api/portal/manager/analysis';
import { getToken } from '@/utils/auth';
export default {
  name: 'AppNewListTempV1',
  props: {
    temp: {
      type: Object,
      default() {
        return {};
      }
    },
    showHeader: {
      type: Boolean,
      default: true
    },
    layout: {
      type: Object,
      default: () => {
        return {};
      }
    }
  },
  data() {
    return {
      loading: false,
      column: [],
      dataList: [],
      cid: null,
      isRefresh: false,
      refreshTime: 30,
      setting: null,
      customStyle: {},
      titleStyle: {},
      bodyStyle: {},
      titleFont: {},
      active: null,
      activeStyle: {}
    };
  },
  computed: {
    borderStyle() {
      if (this.temp.sfdzqp === true) {
        return {
          position: 'absolute',
          left: '0px',
          right: '0px',
          top: '0px',
          bottom: '0px'
        };
      }
      return { height: this.temp.defaultHeight };
    },
    headerStyle() {
      if (this.temp.sfzdy === true) {
        return {
          backgroundColor: this.temp.backgroundColor,
          color: this.temp.fontColor
        };
      }
      return {};
    }
  },
  watch: {
    layout: {
      handler(newData, oldData) {
        this.customStyle = newData;
        this.titleStyle = {
          backgroundColor: newData.mrlb,
          opacity: newData.tmd
        };
        this.titleFont = { color: newData.lmys };
        this.activeStyle = {
          color: newData.xsys,
          backgroundColor: newData.xzbjs
        };
      },
      immediate: true, // 父组件第一次传值时，执行handler，false则第一次不执行
      deep: false // formType为对象时，设置为true，此属性监听对象发生变化
    },
    temp: {
      handler(newData, oldData) {
        if (this.showHeader) {
          const pzx = JSON.parse(newData.data.pzx);
          this.setting = pzx;
          this.column = pzx;
          if (pzx.isRefresh !== undefined && pzx.isRefresh === true) {
            this.isRefresh = pzx.isRefresh;
          }
          if (pzx.refreshTime !== undefined && pzx.refreshTime !== null) {
            this.refreshTime = pzx.refreshTime;
          }
          this.cid = newData.data.id;
          this.initData();
        } else {
          const pzx = JSON.parse(newData.pzx);
          this.setting = pzx;
          if (pzx.isRefresh !== undefined && pzx.isRefresh === true) {
            this.isRefresh = pzx.isRefresh;
          }
          if (pzx.refreshTime !== undefined && pzx.refreshTime !== null) {
            this.refreshTime = pzx.refreshTime;
          }
          this.column = pzx;
          this.cid = newData.id;
          this.initData();
        }
      },
      immediate: true, // 父组件第一次传值时，执行handler，false则第一次不执行
      deep: false // formType为对象时，设置为true，此属性监听对象发生变化
    }
  },
  // todo:: 这里注意下，后期考虑统一修改为data
  methods: {
    initData() {
      this.loading = true;
      if (getToken()) {
        queryAllSystemData({ cid: this.cid })
          .then(res => {
            const { data } = res;
            if (undefined !== data.dataList && data.dataList !== null) {
              this.dataList = data.dataList;
              if (this.isRefresh === true) {
                this.timer = setTimeout(this.initData, this.refreshTime * 1000);
              }
            }
            this.loading = false;
          })
          .catch(() => {
            this.loading = false;
            this.dataList = [];
          });
      } else {
        queryAllGuestSystemData({ cid: this.cid })
          .then(res => {
            const { data } = res;
            if (undefined !== data.dataList && data.dataList !== null) {
              this.dataList = data.dataList;
              if (this.isRefresh === true) {
                this.timer = setTimeout(this.initData, this.refreshTime * 1000);
              }
            }
            this.loading = false;
          })
          .catch(() => {
            this.dataList = [];
            this.loading = false;
          });
      }
    },
    openEntity(data) {
      // 打开详情页
      if (this.column.isOpenEntity) {
        // 允许打开详情页
        const params = [];
        let url = '';
        if (
          undefined !== this.column.entityParams &&
          this.column.entityParams !== null &&
          this.column.entityParams.length > 0
        ) {
          this.column.entityParams.forEach(item => {
            const obj = { code: item.code, value: data[item.value] };
            params.push(obj);
          });
        }
        if (this.column.entityOpenType === 1) {
          // 地址参数的方式打开
          url = data[this.column.entityUrl];
        }
        if (this.column.entityOpenType === 2) {
          // 地址传参查询
          if (
            undefined !== this.column.params &&
            this.column.params !== null &&
            this.column.params.length > 0
          ) {
            url = this.column.entityUrl;
            this.column.params.forEach(item => {
              url = url.replace(
                new RegExp(
                  '\\$\\{([\\s]*?)(' + item.code + '{1})([\\s]*?)([^}])*\\}'
                ),
                data[item.column]
              );
            });
          }
        }
        const object = {
          title: data[this.column['title']],
          params: params,
          setting: this.temp,
          entityTemp: this.column.entityTemplateId,
          temp: this.column.entityTemp,
          openType: this.column.entityOpenType,
          url: url
        };
        this.$emit('fetch-entity', object);
      }
    },
    closeAlert(index) {
      this.active = null;
    },
    openAlert(index) {
      this.active = index;
    },
    callBack() {
      if (this.temp.dkfs === 2) {
        // 自定义打开方式
        if (undefined !== this.temp.path && this.temp.path !== null) {
          window.open(this.temp.path, '_blank');
        }
      } else if (this.temp.dkfs === 1) {
        this.$emit('fetch-data', this.temp);
      } else {
        console.log('');
      }
    }
  }
};
</script>

<style scoped>
.custom-body {
  width: 100%;
  height: 100%;
  position: relative;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.12);
  /* padding: 0px 0px 10px 0px; */
  box-sizing: border-box;
  overflow: hidden;
  border-bottom-left-radius: 6px;
  border-bottom-right-radius: 6px;
}
.body-custom {
  width: 100%;
  height: 100%;
  position: relative;
  display: flex;
  flex-flow: column nowrap;
}
.custom-header {
  width: 100%;
  height: 40px;
  flex: 0 1 auto;
  padding: 0px 10px;
  box-sizing: border-box;
  line-height: 40px;
  cursor: pointer;
  /* border-bottom: 1px solid #ebeef5; */
}
.header-body {
  width: 100%;
  height: 41px;
  display: flex;
  flex-flow: row nowrap;
}
.header-icon {
  width: 20px;
  height: 40px;
  flex: 0 1 auto;
  text-align: center;
  color: #409eff;
}
.header-icon.more {
  width: 40px;
}
.header-title {
  height: 40px;
  flex: 1 1 auto;
}
.header-line {
  position: absolute;
  left: 0px;
  top: 0px;
  height: 40px;
  width: 4px;
  background-color: #0a84ff;
}
.title-span {
  height: 35px;
  padding: 12px 5px;
  box-sizing: border-box;
  line-height: 35px;
  text-align: center;
  position: relative;
  font-weight: bolder;
}
.custom-content {
  width: 100%;
  height: auto;
  flex: 1 1 auto;
  /* padding: 0px 10px; */
  overflow: hidden;
  box-sizing: border-box;
  /* background: #f5f7fa; */
}
.custom-ul-li {
  width: 100%;
  height: 35px;
  display: flex;
  /* flex-flow: column nowrap; */
  border-bottom: 1px solid #ebeef5;
  cursor: pointer;
  flex-flow: row nowrap;
  color: #606266;
  margin-bottom: 2px;
  border-radius: 3px;
  background: #f5f7fa;
}
.custom-ul-li-title {
  font-size: 13px;
  flex: 1 1 auto;
  height: 40px;
  line-height: 40px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  margin-left: 10px;
  width: 70%;
}
.custom-ul-li-button {
  /* flex: 1 1 auto; */
  height: 35px;
  display: flex;
  flex-flow: row nowrap;
  color: #909399;
}
.custom-li-button {
  flex: 0 1 auto;
  overflow: hidden;
  line-height: 35px;
  font-size: 12px;
  margin-right: 8px;
}
.custom-li-time {
  flex: 0 1 auto;
  height: 20px;
  line-height: 20px;
  text-align: right;
  font-size: 12px;
}
[class*="el-col-"] {
  float: left;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
</style>
